<?php
require_once 'BaseController.php';

/**
 * 【PC版】店舗検索(地域・沿線・駅選択) コントローラ
 */
class Renew_ShopsearchController extends Renew_BaseController
{

    /**
     * メインアクション
     */
    public function indexAction()
    {
        $params = $this->getParameters();

        $cat = $params['Category'];
        if (empty($cat)) $cat = 'index';

        $area = $params['Area'];

        if ($cat == 'index')
        {
            $this->makeTemplateParams1($area);
        }
        else if ($cat == 'line')
        {
            $rail = $params['RailCd'];
            if ($rail == '') $cat = 'route';
            $this->makeTemplateParams2($area, $rail);
        }

        $this->displayBody2($cat);
    }

    private function makeTemplateParams1($area)
    {
        $model = new Shop_Model_SelectCity();

        $params = array();
        $prefCd = $this->getPrefCode($area);
        $this->getPrefData($prefCd);

        $params['PrefCd'][] = $prefCd;
        $r1 = $model->getList($params);
        $data = $model->convertData($r1, $params);
        $this->_data['Mapping'] = $data;
        $this->_data['Mapping_area'] = $area;
        $this->_data['Mapping_area_code'] = $this->getPrefCode($area);

        $this->_data['seo_search_data']['KODAWARI'] = Apm_Seo::getSeoImpKodawari(0, 6);
    }

    private function makeTemplateParams2($area, $rail)
    {
        $model = new Shop_Model_SelectRail();
        $params = array();
        $prefCd = $this->getPrefCode($area);
        $this->getPrefData($prefCd);

        $params['PrefCd'][] = $prefCd;
        if (!empty($rail)) $params['RailCd'][] = $rail;

        $r1 = $model->getList($params);
        $data = $model->convertData($r1, $params);
        $this->_data['Mapping'] = $data;
        $this->_data['Mapping_area'] = $area;
        $this->_data['Mapping_area_code'] = $this->getPrefCode($area);

        $this->_data['seo_search_data']['KODAWARI'] = Apm_Seo::getSeoImpKodawari(0, 6);

        // 簡単検索に保存されているクッキーを表示される
        // $simpleEki = $_COOKIE['simpleEki'];
        // if(isset($simpleEki) && !empty($simpleEki) && $simpleEki !== ''){
        // $vals = explode('|', $simpleEki);
        // $eki = $vals[0];

        // $vals2 = explode('-', $vals[1]);
        // $pref = $vals2[0];

        // if($pref == $prefCd){
        // $this->_data['throughEki'] = mb_convert_encoding($eki, 'SJIS', 'UTF-8') ;

        // //路線
        // $ensenparams = array();
        // $temp = explode('.', $vals2[1]);
        // $rel = array();
        // foreach($temp as $s){
        // $rel[] = substr($s, 0, 4);
        // //$ensenparams['RailCd'][] = substr($s, 0, 4);
        // }
        // $ensens = implode(',', $rel);

        // $ensenparams['PrefCd'][] = $pref;
        // $ensenparams['RailCd'] = $rel;

        // __log_debug_array($ensenparams['RailCd']);

        // $enr1 = $model->getList($ensenparams);
        // $endata = $model->convertData($enr1, $ensenparams);

        // $this->_data['throughEnsen'] = $endata;
        // }
        // }
        // 簡単検索に保存されているクッキーを表示される
    }

    public function getPrefData($code)
    {
        $model = new Search_Model_Selareasearch();
        $prefInfo = $model->getSimpleArea($code);
        $this->_data['PrefInfo'] = $prefInfo;
        // tho 20141125　追加
        $roman = $this->getPrefName($code);
        $this->_data['PrefName'] = $roman;
        // tho 20141125　追加 End
        $jp = $this->getPrefNameJP($code);
        $this->_data['PrefNameJP'] = $jp;
        $this->_data['PrefNameJPWithoutPostfix'] = $this->getPrefNameWithoutPostfix($jp);
        $this->_data['RandomPrefs'] = $this->getRandomPrefs(5);

        // tho 20141210 追加
        $tenpoNinki = new Renew_Model_TenpoNinki();
        $list = $tenpoNinki->getTenpoNinkiArea();

        foreach ($list as $i => $shiku)
        {
            $list[$i]['PrefName'] = $this->getPrefName($shiku['M05_TENPO_TODOUFUKEN_CD']);
        }
        $this->_data['RandomShiku'] = $list;
        // tho 20141210 追加 end
    }

    /**
     * SEO対策用のmetaタグに配置する文章を設定
     */
    protected function setSeoMeta()
    {
        $params = $this->getParameters();

        $prefNameJP = $this->_data['PrefNameJPWithoutPostfix'];

        $this->_meta_title = $prefNameJP . 'のアパマンショップを探す 【アパマンショップ】';
        $this->_meta_description = $prefNameJP . 'のアパマンショップでお部屋を探すなら、不動産情報が満載のアパマンショップネットワーク。賃貸マンション・アパートなど、';
        $this->_meta_keywords = $prefNameJP . ' アパマンショップ,';

        if ($params['Category'] == 'line')
        {
            $this->_alternate_url = 'sp/' . $params['Area'] . '/shop/line/';
        }
        else
        {
            $this->_alternate_url = 'sp/' . $params['Area'] . '/shop/';
        }
    }

}
?>
